package com.offcn.project.service.impl;

import com.alibaba.fastjson.JSON;
import com.offcn.dycommon.enums.ProjectStatusEnume;
import com.offcn.project.contants.ProjectConstant;
import com.offcn.project.enums.ProjectImageTypeEnume;
import com.offcn.project.mapper.*;
import com.offcn.project.po.*;
import com.offcn.project.service.ProjectCreateService;
import com.offcn.project.vo.req.ProjectRedisStorageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@Service
@Slf4j
public class ProjectCreateServiceImpl implements ProjectCreateService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private TProjectMapper projectMapper;

    @Autowired
    private TProjectImagesMapper projectImagesMapper;

    @Autowired
    private TProjectTagMapper projectTagMapper;

    @Autowired
    private TProjectTypeMapper projectTypeMapper;

    @Autowired
    private TReturnMapper returnMapper;
    @Override
    public String initProjectCreate(Integer memberId) {
       //1、准备一个项目token
        String token = UUID.randomUUID().toString().replace("-", "");

        //2、创建项目临时对象存储到redis
        ProjectRedisStorageVo vo = new ProjectRedisStorageVo();

        //3、设置vo用户编号属性
        vo.setMemberid(memberId);

        //4、把项目临时对象转换为json字符串
        String jsonString = JSON.toJSONString(vo);
        //5、把项目json字符串存储到redis
        redisTemplate.opsForValue().set(ProjectConstant.TEMP_PROJECT_PREFIX+token,jsonString);



        return token;
    }

    @Override
    public void saveProjectInfo(ProjectStatusEnume auth, ProjectRedisStorageVo vo) {

        //1、创建一个项目po
        TProject project = new TProject();
        //设置项目状态
        project.setStatus("1");
        //2、把vo的属性值复制到po
        BeanUtils.copyProperties(vo,project);
        log.info("-----money-----:{}-----:{}",vo.getMoney(),project.getMoney());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        //设置项目创建日期
        project.setCreatedate(dateFormat.format(new Date()));
        //3、保存项目基本信息到数据库
        projectMapper.insertSelective(project);

        //4、获取项目编号
        Integer projectId = project.getId();

        //5、获取项目头图
        String headerImage = vo.getHeaderImage();

        TProjectImages headImage = new TProjectImages(null,projectId,headerImage, ProjectImageTypeEnume.HEADER.getCode());
        //保存头图到数据库
        projectImagesMapper.insertSelective(headImage);

        //6、获取项目详情图
        List<String> detailsImages = vo.getDetailsImage();
        //遍历详情图的集合
        for (String detailsImage : detailsImages) {
            //创建详情图对象
            TProjectImages projectImages = new TProjectImages(null, projectId, detailsImage, ProjectImageTypeEnume.DETAILS.getCode());

            //保存详情图信息到数据库
            projectImagesMapper.insertSelective(projectImages);
        }

        //7、获取项目标签
        List<Integer> tagids = vo.getTagids();
        //遍历项目标签
        for (Integer tagid : tagids) {
            //创建标签对象
            TProjectTag projectTag = new TProjectTag(null, projectId, tagid);
            //保存标签对象到数据库
            projectTagMapper.insertSelective(projectTag);
        }

        //8、项目类型
        List<Integer> typeids = vo.getTypeids();
        //遍历项目类型
        for (Integer typeid : typeids) {
            //创建项目类型对象
            TProjectType projectType = new TProjectType(null, projectId, typeid);
            //保存项目类型到数据库
            projectTypeMapper.insertSelective(projectType);
        }

        //5、保存回报信息
        List<TReturn> returns = vo.getProjectReturns();
        //设置项目的id
        for(TReturn tReturn:returns){
            tReturn.setProjectid(projectId);
            returnMapper.insertSelective(tReturn);
        }


        //9、删除保存到redis的项目临时信息
        redisTemplate.delete(ProjectConstant.TEMP_PROJECT_PREFIX+vo.getProjectToken());
    }
}
